package util

import (
	"path/filepath"
	"os"
	"fmt"
	"baliance.com/gooxml/document"
	"bufio"
	"strings"
)

func WriteWord(path string) {
	doc := document.New()
	err := filepath.Walk(path, func(path string, f os.FileInfo, err error) error {
		if f == nil {
			return nil
		}
		if f.IsDir() {
			return nil
		}
		file, err := os.Open(path)
		defer func() {
			err := file.Close()
			if err != nil {
				//_ = log.Error(err)
				fmt.Println(err)
			}
		}()
		if err != nil {
			panic(err)
		}
		buff := bufio.NewReader(file)
		for {
			str, err := buff.ReadString('\n')
			if err != nil {
				if !(err.Error() == "EOF") {
					fmt.Println(err)
					os.Exit(1)
				}
			}
			str = strings.Replace(str, "\n", "", -1)
			if err != nil && err.Error() == "EOF" {
				fmt.Println(err)
				break
			}
			para := doc.AddParagraph()
			run := para.AddRun()
			run.AddText(str)
		}
		if err != nil {
			fmt.Print("read file error")
		}

		return nil
	})
	doc.SaveToFile("程序源码.docx")
	if err != nil {
		fmt.Printf("filepath.Walk() returned %v\n", err)
	}
}
